/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agh.air.bpel2bpmn.comparator;

import org.agh.air.bpel2bpmn.exception.PatternNotFoundException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 *
 * @author paletz
 */
public abstract class AbstractComparator {

    public abstract String compare(Node node) throws PatternNotFoundException;
    public final String LTMP = "LTMP";
    public final String RTMP = "RTMP";
    protected final String INVOKE = "invoke";
    protected final String FLOW = "flow";
    protected final String SEQUENCE = "sequence";
    protected final String CASE = "case";
    protected final String SWITCH = "switch";
    protected final String OTHERWISE = "otherwise";
    protected final String EXCLUSIVE_CHOICE = "exclusive_choice";
    protected final String SIMPLE_MERGE = "simple_merge";
    protected final String PICK = "pick";
    protected final String ON_MESSAGE = "onMessage";
    protected final String ON_ALARM = "onAlarm";

    protected String getNameAttribute(Node node) throws PatternNotFoundException {
        return getAttributeByName(node, "name");
    }

    protected Node getFirstChild(Node node) throws PatternNotFoundException {
        if (node.getChildNodes().getLength() > 0) {
            return node.getChildNodes().item(0);
        } else {
            throw new PatternNotFoundException();
        }
    }

    protected Node getSecondChild(Node node) throws PatternNotFoundException {
        if (node.getChildNodes().getLength() > 1) {
            return node.getChildNodes().item(1);
        } else {
            throw new PatternNotFoundException();
        }
    }

    protected Node getIChild(Node node, int index) throws PatternNotFoundException {
        if (node.getChildNodes().getLength() > index) {
            return node.getChildNodes().item(index);
        } else {
            throw new PatternNotFoundException();
        }
    }

    protected String getAttributeByName(Node node, String str) {
        NamedNodeMap nnm = node.getAttributes();
        return nnm.getNamedItem(str).getNodeValue();
    }
}
